package com.qf.shiro;

import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminService;
import com.qf.service.PermService;
import com.qf.service.RoleService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author hanjiejie
 * @version V1.0
 * @Project dk2204
 * @Package com.qf.shiro
 * @Description:
 * @Date 2022/7/25 19:45
 *
 */
public class AdminAuthorizingRealm extends AuthorizingRealm {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private PermService permService;
    /**
     * 授权方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取用户对象
        DtsAdmin admin =(DtsAdmin) getAvailablePrincipal(principalCollection);
        Integer[] roleIds = admin.getRoleIds();
        //根据用户的id，到数据库角色表中获取用户对应的集合
        Set<String> roles = roleService.queryRolesByIds(roleIds);
        //根据角色获取用户对应的权限集合

        Set<String> perm = permService.queryPermByRoleIds(roleIds);
        //创建shiro框架要求的授权对象
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //将用户的角色和权限放入shiro框架的授权对象中
            info.setRoles(roles);
            info.setStringPermissions(perm);
        //返回shiro的授权对象
        return info;
    }

    /**
     * 认证方法 判断用户密码是否正确
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1.获取用户输入的用户名，密码
       UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        String pwd =new String(token.getPassword());
        //2.根据用户名到数据库中查询对应的用户对象
        List<DtsAdmin> adminList = adminService.findAdminByName(username);

        //3.判断用户查询到的用户对象是否正确
            if (adminList ==null || adminList.size() ==0){
                throw new UnknownAccountException("找不到该用户，用户名有误");
            }
        //4.判断密码是否正确
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        ///Bcrypt 工具类进行校验， 密码使用的BCrypt算法进行加密
        //第一参数是明文密码，第二参数是加密后的密码，返回校验结果
          if (!encoder.matches(pwd, adminList.get(0).getPassword())){
              throw new UnknownAccountException("密码不正确");
          }

        //5.封装shiro指定的用户对象返回给shiro框架处理
        AuthenticationInfo info = new SimpleAuthenticationInfo(adminList.get(0),pwd,this.getName());
        System.out.println("1111");
        return info;
    }
}
